package com.esp.parser;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import javax.xml.transform.stream.StreamSource;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

import com.esp.Item;
import com.esp.Result;

public class LinkShareItemGenerator {
	private Jaxb2Marshaller jaxb2Marshaller;
	
	public List<Item> getItemsForSearch(String searchString) throws IOException {
		Result result = new Result();
		String uri =
            "http://productsearch.linksynergy.com/productsearch?token=" +
            "ff302c7e08c8d1018c84a71a7982ebe427455a2db131ec72402f70b9ab908ef7&keyword=" + searchString;
        URL url = new URL(uri);
        HttpURLConnection connection =(HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/xml");
		try {
			InputStream xml = connection.getInputStream();
			result = (Result) jaxb2Marshaller.unmarshal(new StreamSource(xml));
		} finally {
		   connection.disconnect();
		}
		return result.getItem();
	}
	
	@Required
	public void setJaxb2Marshaller(Jaxb2Marshaller jaxb2Marshaller) {
		this.jaxb2Marshaller = jaxb2Marshaller;
	}
}
